import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:robot3/pojo.dart';
import 'package:robot3/services.dart';
import 'package:robot3/xiangqing.dart';

import 'widgets.dart';

// https://docs.flutter.dev/get-started/fundamentals/networking#introduction-to-retrieving-data-over-the-network
// https://docs.flutter.dev/cookbook/networking/fetch-data
// https://api.flutter.dev/flutter/painting/BoxDecoration-class.html

void main() {
  runApp(const MyHttp());
}

class MyHttp extends StatelessWidget {
  const MyHttp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const HttpHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class HttpHomePage extends StatefulWidget {
  const HttpHomePage({super.key, required this.title});

  final String title;

  @override
  State<HttpHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<HttpHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body:Row(
        children: [
          SizedBox(height: 200),
          SizedBox(width: 10),
        MyButton01(
            text: '返回',
            containerColor: Colors.black,
            textColor: Colors.white,
            size: Size(200, 50),
            fontSize: 16.0,
            fontWeight: FontWeight.bold,
            onClick: (text) {
              SystemNavigator.pop();
              // Navigator.of(context).pop();
            },
          ),
          SizedBox(width: 4),
          MyButton01(
            text: 'Login',
            containerColor: Colors.black,
            textColor: Colors.white,
            size: Size(200, 50),
            fontSize: 16.0,
            fontWeight: FontWeight.bold,
            onClick: (text) {
              var a = login();
              a.then((value) {
                print("then: ${value}");  // 输出 login() 的返回值
              });
            },
          ),
          SizedBox(width: 4),
          MyButton01(
            text: '网络请求get',
            containerColor: Colors.black,
            textColor: Colors.white,
            size: Size(200, 50),
            fontSize: 16.0,
            fontWeight: FontWeight.bold,
            onClick: (text) {
              print('Button pressed: $text');
            },
          ),
          SizedBox(width: 4),
          RobotButton(onClick: (text){

          },),
        ],
      )
    );
  }
}

// Future<int> login() async {
//   var url = Uri.https(host, 'robot/login');
//   var response = await http.post(url, body: {'sn': 'doodle'});
//   var a =  ApiResponse<LoginBean>.fromJson(jsonDecode(response.body));
//   print(a.data.toString());
//   if(a.data?.userId==null){
//     print('请扫码');
//     return 0;
//   }
//   return 1;
// }

const String host = 'applet.xmlights.com';
